package learn;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Layouts {

	static ArrayList<JButton> buttons = new ArrayList<JButton>();
	static Iterator<JButton> iButtons;
	static JButton b;
	
	public Layouts(){
		
		final JFrame window = new JFrame("Test Layoutów");
		
		ActionListener actionListener = new ActionListener() {	
			@Override
			public void actionPerformed(ActionEvent e) {
				buildJFrameWithLayout(e.getActionCommand());
			}
		};
			
			
		buttons.add(new JButton("GridLayout"));
		buttons.add(new JButton("BorderLayout"));
		buttons.add(new JButton("FlowLayout"));
		buttons.add(new JButton("BoxLayout"));
		
		iButtons = buttons.iterator();
		while ( iButtons.hasNext() ){
			b = (JButton)iButtons.next();
			b.addActionListener(actionListener);
			window.add(b);
		}
		
		window.setLayout( new GridLayout() );
		
		window.pack();
		window.setVisible(true);
	}
	
	public void buildJFrameWithLayout(String layout){
		JFrame w = new JFrame();
		
		for (int i=0; i < 10; i++){
			w.add(new JButton(String.valueOf(i)));
		}
		
		switch(layout){
		case "GridLayout": 
			w.setLayout( new GridLayout() );
			break;
		case "BorderLayout":
			w.setLayout( new BorderLayout() );
			break;
		case "FlowLayout":
			w.setLayout( new FlowLayout() );
			break;
		case "BoxLayout":
			break;
		}
		w.pack();
		w.setVisible(true);
	}
	
}
